<?php

function generaGridPHP($meta){

$atributs = $meta->getAtributs();
$atributs = explode(" ,", $atributs);
$tipus = $meta->getTipus();
$tipus = explode(" ,", $tipus);

$taula = $meta->getTaula();
$pk = $meta->getPk();
$idiomes = $meta->getNumIdiomes();

$variable = "<?php
header(\"Content-type: text/html; charset=utf-8\");
include('autenticacion.php');
include('funciones.php');
require_once('Database.class.php');
\$db = Database::getInstance();
\$db->getConnection();

\$task = '';
if ( isset(\$_POST['task']) ){
	\$task = \$_POST['task'];
}
  
switch( \$task ){
    case 'LIST':
		getList();
    break;
	case 'UPDATE':
		update();
    break;
	case 'DELETE':
		delete();
    break;
	case 'ADD':
		add();
    break;
	case 'ID2':
		id2();
    break;
    default:
		echo '{failure:true}';
    break;
}

include('desconnexion.php');

function getList() {

	//remoteSort
	\$ordenacio = '';
	if( isset(\$_POST['sort']) and isset(\$_POST['dir']) ){
		\$ordenacio = \"ORDER BY \".\$_POST['sort'].\" \".\$_POST['dir'];
	}

	\$query = \"SELECT * FROM $taula WHERE idioma=1 \$ordenacio\";
	\$result = mysql_query(\$query);
	\$nbrows = mysql_num_rows(\$result);	

	\$start = (integer) (isset(\$_POST['start']) ? \$_POST['start'] : \$_GET['start']);
	\$end = (integer) (isset(\$_POST['limit']) ? \$_POST['limit'] : \$_GET['limit']);
	\$limit = \"\$query LIMIT \$start,\$end\";		
	\$result = mysql_query(\$limit);
	  
	if(\$nbrows>0){
		while(\$rec = mysql_fetch_array(\$result)){
";
			for( $i=0; $i<count($atributs); $i++ ){
				switch ( $tipus[$i] ){
					case 'string':
					break;
					case 'blob':
						//$variable .= "\t\t\t\$rec['$atributs[$i]']=parserAccentsTextAHtml(\$rec['$atributs[$i]']);\n";
					break;
					case 'date':
						$variable .= "\t\t\t\$rec['$atributs[$i]']=codeDate(\$rec['$atributs[$i]']);\n";
					break;
					case 'time':
						$variable .= "\t\t\t\$rec['$atributs[$i]']=codeTime(\$rec['$atributs[$i]']);\n";
					break;
					default:
					break;
				}
			}
$variable .= "			
			\$arr[] = \$rec;
		}
		\$jsonresult = JEncode(\$arr);
		echo '({\"total\":\"'.\$nbrows.'\",\"results\":'.\$jsonresult.'})';
	} else {
		echo '({\"total\":\"0\", \"results\":\"\"})';
	}
}

function id2() {
	\$id2 = \$_POST['id2'];

	\$query = \"SELECT * FROM $taula WHERE id2 = \$id2 ORDER BY idioma ASC LIMIT $idiomes\";
	\$result = mysql_query(\$query);
	\$nbrows = mysql_num_rows(\$result);	
	  
	if(\$nbrows==$idiomes){
		while(\$rec = mysql_fetch_array(\$result)){
";
		for( $i=0; $i<count($atributs); $i++ ){
			switch ( $tipus[$i] ){
				case 'string':
				break;
				case 'blob':
					//$variable .= "\t\t\t\$rec['$atributs[$i]']=parserAccentsTextAHtml(\$rec['$atributs[$i]']);\n";
				break;
				case 'date':
					$variable .= "\t\t\t\$rec['$atributs[$i]']=codeDate(\$rec['$atributs[$i]']);\n";
				break;
				case 'time':
					$variable .= "\t\t\t\$rec['$atributs[$i]']=codeTime(\$rec['$atributs[$i]']);\n";
				break;
				default:
				break;
			}
		}
$variable .= "	
			\$arr[] = \$rec;
		}
		\$jsonresult = JEncode(\$arr);
		echo '({\"total\":\"'.\$nbrows.'\",\"results\":'.\$jsonresult.'})';
	} else {
		echo '({\"total\":\"0\", \"results\":\"\"})';
	}
}

function update() {	
";
	for( $i=0; $i<count($atributs); $i++ ){
	//PK o ID2 o IDIOMA
	if( $atributs[$i] == $pk or $atributs[$i] == "id2" or $atributs[$i] == "idioma" ){
		$variable .= "\t$$atributs[$i] = \$_POST['$atributs[$i]'];\n";
	}
}

$variable .= "\n";


	$variable_aux = "\t\$query = \"UPDATE $taula SET ";
	for( $i=0; $i<count($atributs); $i++ ){
		//ALTRES
		if( $atributs[$i] != $pk and $atributs[$i] != "id2" and $atributs[$i] != "idioma" ){
			switch ( $tipus[$i] ){
				case 'string':
					$variable .= "\t$$atributs[$i] = fixMagicQuotes(\$_POST['$atributs[$i]']);\n";
					$variable_aux .= "$atributs[$i]='$$atributs[$i]', ";
				break;
				case 'blob':
					$variable .= "\t$$atributs[$i] = fixMagicQuotes(\$_POST['$atributs[$i]']);\n";
					$variable_aux .= "$atributs[$i]='$$atributs[$i]', ";
				break;
				case 'time':
					$variable .= "\t$$atributs[$i] = decodeTime(\$_POST['$atributs[$i]']);\n";
					$variable_aux .= "$atributs[$i]='$$atributs[$i]', ";
				break;
				case 'date':
					$variable .= "\t$$atributs[$i] = decodeDate(\$_POST['$atributs[$i]']);\n";
					$variable_aux .= "$atributs[$i]='$$atributs[$i]', ";
				break;
				case 'int':
					$variable .= "\t$$atributs[$i] = \$_POST['$atributs[$i]'];\n";
					$variable_aux .= "$atributs[$i]=$$atributs[$i], ";
				break;
				case 'real':
					$variable .= "\t$$atributs[$i] = \$_POST['$atributs[$i]'];\n";
					$variable_aux .= "$atributs[$i]='$$atributs[$i]', ";
				break;
				default:
					$variable .= "\t$$atributs[$i] = fixMagicQuotes(utf8_decode(\$_POST['$atributs[$i]']));\n";
					$variable_aux .= "$atributs[$i]='$$atributs[$i]', ";
				break;
			}
		}
	}
	$variable_aux = substr ($variable_aux, 0, -2); //eliminem ,  de l'ultim camp
	$variable_aux .= " WHERE id2 = \$id2 AND idioma = \$idioma\";\n";
	$variable_aux .= "\t\$result = mysql_query(\$query);\n\n";
	$variable .= $variable_aux;
$variable .= "	
    echo '1'; 
}

function add(){
	\$query = \"SELECT MAX(id2) as 'lastid' FROM $taula\";
	\$result = mysql_query(\$query);
	if( isset(\$result) ){ 
		\$rec = mysql_fetch_array(\$result);
		\$id2=\$rec['lastid']+1;
		for(\$j=1;\$j<=$idiomes;\$j++){
			\$query2 = \"INSERT INTO $taula (id2 ,idioma) VALUES (\$id2 , \$j)\";
			\$result2 = mysql_query(\$query2);
		}
		echo '1';
	}else{
		echo '0';
	}
}

function delete(){
	\$ids = \$_POST['ids'];
	if (version_compare(PHP_VERSION,'5.2','<'))  {    
		require_once('JSON.php'); 
		\$json = new Services_JSON();
		\$id2 = \$json->decode(stripslashes(\$ids));
	} else {
		\$id2 = json_decode(stripslashes(\$ids));
	}
   
    if(sizeof(\$id2)<1){
		echo '0';
    } else if (sizeof(\$id2) == 1){
		\$query = \"DELETE FROM $taula WHERE id2 = \$id2[0] LIMIT $idiomes\";
		mysql_query(\$query);
		echo '1';
    } else {
		\$query = \"DELETE FROM $taula WHERE \";
		for(\$i = 0; \$i < sizeof(\$id2); \$i++){
			\$query = \$query . \"id2 = \".\$id2[\$i];
			if(\$i<sizeof(\$id2)-1){
				\$query = \$query . \" OR \";
			}     
		}
		//\$query = \$query . \" LIMIT $idiomes \";
		mysql_query(\$query);
		echo '1';
    }  
}

?>
";

return $variable;
}

?>